package com.tsingteng.cosfun.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.utils.StringUtils;
import com.tsingteng.cosfun.widget.CompletedView;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/10/6
 * qq(872179966)
 */

public class ProgressDialog  {

    private Activity context;
    private Dialog dialog;
    private CompletedView mProgressbar;
    private TextView mTvProgressbar;


    public ProgressDialog(Activity context) {
        this.context = context;
        initDialog();
    }

    private void initDialog() {
        dialog = new Dialog(context, R.style.commselectDialog);
        dialog.setContentView(R.layout.download_progress_dialog);
        mProgressbar = dialog.findViewById(R.id.download_progressbar);
        mTvProgressbar = dialog.findViewById(R.id.download_progress);
        mProgressbar.setMaxProgress(100);
        dialog.setCanceledOnTouchOutside(false);

    }


    public void show() {
        if (context.isFinishing())return;
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        mProgressbar.setProgress(0);
        mTvProgressbar.setText("0%");
        mProgressbar.setAlpha(0.4f);
        dialog.show();
    }


    public void setMaxProgressBar(int maxProgressBar) {
        mProgressbar.setMaxProgress(maxProgressBar);
    }

    public void setmProgressbar(int progressbar) {
        mProgressbar.setProgress(progressbar);
        float alpha = (float) StringUtils.div(progressbar, 100, 1);
        if (progressbar != 0) {
            if (alpha<0.4) {
                mProgressbar.setAlpha(0.4f);
            }else {
                mProgressbar.setAlpha(alpha);
            }
        }
        mTvProgressbar.setText(progressbar+"%");
    }



    public void disMissDialog() {

        if (context !=null && dialog!= null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }





}
